﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using TriviaViewModel.ServiceReference1;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using System.IO;
using System.Windows.Media.Imaging;

namespace TriviaViewModel
{
    public class QuestionWizardViewModel : ViewModelBase
    {
        int numberOfCheckBoxesClicks;
        #region Properties
        /// <summary>
        /// question added to db
        /// </summary>
        private string _questionAdded;
        public string QuestionAdded
        {
            get { return _questionAdded; }
            set
            {
                if (_questionAdded != value)
                {
                    _questionAdded = value;
                    RaisePropertyChanged("QuestionAdded");
                }
            }
        }

   

        private bool _isRightAnswer;
        public bool IsRightAnswer
        {
            get { return _isRightAnswer; }
            set
            {
                if (_isRightAnswer != value)
                {
                    _isRightAnswer = value;
                    RaisePropertyChanged("IsRightAnswer");
                }
            }
        }

        private BitmapSource _questionAddedImageSource;
        public BitmapSource QuestionAddedImageSource
        {
            get { return _questionAddedImageSource; }
            set
            {
                if (_questionAddedImageSource != value)
                {
                    _questionAddedImageSource = value;
                    RaisePropertyChanged("QuestionAddedImageSource");
                }
            }
        }

       
        /// <summary>
        /// difficulty for  question added
        /// </summary>
        private string _selectedDifficulty;
        public string SelectedDifficulty
        {
            get { return _selectedDifficulty; }
            set
            {
                if (_selectedDifficulty != value)
                {
                    _selectedDifficulty = value;
                    RaisePropertyChanged("SelectedDifficulty");
                }
            }
        }

        /// <summary>
        /// topic for question added
        /// </summary>
        private string _selectedTopic;
        public string SelectedTopic
        {
            get { return _selectedTopic; }
            set
            {
                if (_selectedTopic != value)
                {
                    _selectedTopic = value;
                    RaisePropertyChanged("SelectedTopic");
                }
            }
        }

        private ObservableCollection<AnswerCalss> _answers;
        public ObservableCollection<AnswerCalss> Answers
        {
            get { return _answers; }
            set
            {
                if (_answers != value)
                {
                    _answers = value;
                    RaisePropertyChanged("Answers");
                }
            }
        }
        private string _clue;
        public string Clue
        {
            get { return _clue; }
            set
            {
                if (_clue != value)
                {
                    _clue = value;
                    RaisePropertyChanged("Clue");
                }

            }
        }
        /// <summary>
        /// game topics
        /// </summary>
        private List<string> _topics;
        public List<string> Topics
        {
            get { return _topics; }
            set
            {
                if (_topics != value)
                {
                    _topics = value;
                    RaisePropertyChanged("Topics");
                }
            }
        }
        /// <summary>
        /// game difficulties
        /// </summary>
        private List<string> _difficulties;
        public List<string> Difficulties
        {
            get { return _difficulties; }
            set
            {
                if (_difficulties != value)
                {
                    _difficulties = value;
                    RaisePropertyChanged("Difficulties");
                }
            }
        }

        #endregion

       
        public ICommand AddQuestionDetailsCommand { get; set; }
        public ICommand AddAnswerCommand { get; set; }
        public ICommand CheckedCommand { get; set;  }
        public ICommand AddPictureCommand { get; set; }

        public QuestionWizardViewModel()
        {
            Answers = new ObservableCollection<AnswerCalss>();
            using (var proxy = new ServiceReference1.GameServiceClient())
            {
                Topics = proxy.GetTopics().ToList();
                Difficulties = proxy.GetDificulties().ToList();
            }

            AddQuestionDetailsCommand = new RelayCommand(() => AddQuestionCommandExecute());
            AddAnswerCommand = new RelayCommand(() => AddAnswerCommandExecute());
            CheckedCommand = new RelayCommand(() => CheckedCommandExecute());
            AddPictureCommand = new RelayCommand(() => AddPictureCommandExecute());


        }

      

       

        private void AddQuestionCommandExecute()
        {
            if (SelectedDifficulty != null && SelectedTopic != null && QuestionAdded != null)
            {
                int qID;
                using (var proxy = new GameServiceClient())
                {
                    
                    QuestionCalss question = new QuestionCalss()
                    {
                         DificultyId = proxy.GetDifficultyId(SelectedDifficulty),
                         TypeId = proxy.GetTypeId(SelectedTopic),
                         QuestionString = QuestionAdded,
                         Clue = (this.Clue!=null)?this.Clue: ""

                    };
                    qID = proxy.AddQuestion(question);

                    if (Answers != null)
                    {
                        foreach (var item in Answers)
                            item.QuestionId = qID;
                    }
                    proxy.AddAnswers(Answers.ToArray());
                }
                Answers.Clear();
                QuestionAdded = null;
               
                Clue = null;
                SelectedTopic = null;
                SelectedDifficulty = null;
            }
        }

        private void AddAnswerCommandExecute()
        {
        //    if (AnswerAdded != null)
        //    {
        //        AnswerCalss answer = new AnswerCalss();
        //        answer.AnswerString = AnswerAdded;
        //        answer.IsRightAnswer = IsRightAnswer;
        //        Answers.Add(answer);
        //        AnswerAdded = null;
        //    }
           

        }

        private void CheckedCommandExecute()
        {
            if (numberOfCheckBoxesClicks % 2 == 0)
                IsRightAnswer = false;
            else
                IsRightAnswer = true;
        }

        private void AddPictureCommandExecute()
        {
            OpenFileDialog fileloader = new OpenFileDialog();
            fileloader.ShowDialog();
            fileloader.DefaultExt = ".png";

            using (FileStream fileStream = File.OpenRead(fileloader.FileName))
            {
                MemoryStream memStream = new MemoryStream();
                memStream.SetLength(fileStream.Length);
                fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);

                var img = new BitmapImage(new Uri(fileloader.FileName));
               // _questionAddedImageSource = new BitmapSource(new Uri(fileloader.FileName)));



            }
        }

    }
}
